﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StuLib;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ArrayList al = null;
        Hashtable ht = null;
        List<int> list = null;
        Dictionary<string, Student> dicStus = null;

        //List<object> === ArrayList
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            al = new ArrayList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //al[idx]  if idx > al.Count - 1 error
            al.Add(12);
            al.Add(45);
            al.Add(23);
            al.Add(23);
            al.Add(45);
            al.Add(23);
            al.Add("test");
            al.Insert(0, 67);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            al.Remove("test");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //foreach遍历List时，不要增减元素
            //foreach (object obj in al)
            //{
            //    if (obj.Equals("test"))
            //        al.Remove(obj);
            //}
            //对于List，删除元素原则上建议倒序删除，否则可能引发逻辑错误(尽量不在for循环内修改i的值，非强制，仅提倡)
            for (int i = al.Count - 1; i > 0; i--)
            {
                if (al[i].Equals(23))
                    al.Remove(al[i]);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (object obj in al)
            {
                listBox1.Items.Add(obj);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ht = new Hashtable();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //Add  调用Add试图添加已存在的key，程序报错
            ht.Add("01", "张三");
            ht.Add(25, sender);
            ht.Add("02", "刘备");
            ht.Add("03", "关羽");
            //ht[key] = value;1、如果key值不存在，调用Add添加；2、如果Key值存在，改写对应key的value
            //ht[25] = "test";
            ht["04"] = "张飞";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            //public DictionaryEntry(object key, object value);就是键值对
            //可以理解为Hashtable就是由它组成
            foreach (DictionaryEntry de in ht)
            {
                string fmt = "key = {0}  val = {1}";
                listBox1.Items.Add(string.Format(fmt, de.Key, de.Value));
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (object key in ht.Keys)
            {
                string fmt = "key = {0}  val = {1}";
                listBox1.Items.Add(string.Format(fmt, key, ht[key]));
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ht.Remove("01");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            list = new List<int>();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            list.Add(12);
            list.Add(45);
            list.Add(23);
            list.Add(23);
            list.Add(45);
            list.Add(23);
            list.Insert(0, 67);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (int i in list)
            {
                listBox1.Items.Add(i);
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            dicStus = new Dictionary<string, Student>();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            //dicStus[key] = value;1、如果key值不存在，调用Add添加；2、如果Key值存在，改写对应key的value
            dicStus["01"] = new Student()
            {
                Age = 45,
                SName = "张飞",
                Sno = "01"
            };

            dicStus["02"] = new Student("关羽", "02", 48);

            Student s = new Student();
            s.Sno = "03";
            s.SName = "刘备";
            s.Age = 56;
            dicStus[s.Sno] = s;

            dicStus.Add("04", new Student("孔明", "04", 23));
        }

        private void button21_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (KeyValuePair<string, Student> kvp in dicStus)
            {
                listBox1.Items.Add(kvp.Value.HelloMsg);
            }
            //Students ss = new Students();
            //ss["01"] = new Student("关羽", "02", 48);
            //Student s = ss["01"];

            //ss["01"] = null;
        }

        Students ss = null;
        private void button16_Click(object sender, EventArgs e)
        {
            ss = new Students();
        }

        private void button22_Click(object sender, EventArgs e)
        {
            ss["01"] = new Student("关羽", "01", 45);
            ss["02"] = new Student("张飞", "02", 48);
            ss["03"] = new Student("刘备", "03", 56);
            ss["04"] = new Student("张飞", "04", 56);
        }

        private void button25_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (string sno in ss.Snoes)
            {
                listBox1.Items.Add(ss[sno].HelloMsg);
            }
        }

        private void button23_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                foreach (Student s in ss[textBox1.Text, 0])
                {
                    ss[s.Sno] = null;
                }
            }
        }

        private void button24_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            if (textBox1.Text != "")
            {
                foreach (Student s in ss[textBox1.Text, 0])
                {
                    listBox1.Items.Add(s.HelloMsg);
                }
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            //删除指定key
            if (!string.IsNullOrEmpty(textBox1.Text))
                ss[textBox1.Text] = null;
        }
    }
}
